package com.example.myblog_springboot.controller;

import com.example.myblog_springboot.pojo.LoginInfo;
import com.example.myblog_springboot.result.Result;
import com.example.myblog_springboot.service.MyblogUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.RedissonSemaphore;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Controller
@RequestMapping("/login")
@Tag(name = "用户登录接口")
@CrossOrigin(origins = "*")
@Slf4j
public class MyBlogLoginController {

    @Autowired
    private MyblogUserService myblogUserService;

    @RequestMapping(value = "/user",method = RequestMethod.POST)
    @Operation(summary = "用户注册")
    @ResponseBody
    public Result<List<String> > register(@RequestBody LoginInfo loginInfo) {
        List<String>  mapList = myblogUserService.getUserInfo();
        System.out.println("mapList = " + mapList);
        return Result.success("注册成功",mapList);
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    @Operation(summary = "用户登录")
    @ResponseBody
    public Result<String> login(@RequestBody LoginInfo loginInfo) {
        String blogAccount = loginInfo.getBlogaccount();
        String password = loginInfo.getPassword();

        if (blogAccount == null || password == null){
            return Result.fail("用户名或密码不能为空");
        }
        if (!blogAccount.equals("admin") || !password.equals("123456")){
            return Result.fail("用户名或密码错误");
        }
        if (!loginInfo.getCaptcha().equals("1234")){
            return Result.fail("验证码错误");
        }
        String token = "token";
        token = UUID.randomUUID().toString().replaceAll("-", "");
        return Result.success("登录成功",token);
    }


    @Autowired
    private RedissonClient redissonClient;

    // 限流信号量
    private RSemaphore semaphore;


    @PostConstruct
    public void init() {
        semaphore = redissonClient.getSemaphore("captcha_limit");
        semaphore.trySetPermits(5);
    }

    @RequestMapping(value = "/captcha", method = RequestMethod.GET)
    @Operation(summary = "生成验证码")
    @ResponseBody
    public Result<String> captcha() {
        String captchaCode = null;
        boolean acquired = semaphore.tryAcquire();
        if (acquired) {
            try {
                // 生成验证码业务逻辑
                 captchaCode = UUID.randomUUID().toString().substring(0, 6);
                 Thread.sleep(2000L);
            }catch (InterruptedException e){
                log.error("限流判断被中断", e);
            } finally {
                semaphore.release();
            }
        } else {
            // 未获取到许可，处理失败逻辑
            return Result.fail("你到达最大并发量，请稍后再试");
        }
        return Result.success("获取验证码成功", captchaCode);
    }

}
